package com.yougou.ui.mine.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.yougou.R;
import com.yougou.data.model.Goods;

import java.util.ArrayList;
import java.util.List;

public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.ViewHolder> {
    
    private List<Goods> mData = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;
    private OnDeleteClickListener mOnDeleteClickListener;
    
    public interface OnItemClickListener {
        void onItemClick(Goods goods);
    }
    
    public interface OnDeleteClickListener {
        void onDeleteClick(Goods goods, int position);
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    
    public void setOnDeleteClickListener(OnDeleteClickListener listener) {
        this.mOnDeleteClickListener = listener;
    }
    
    public void setData(List<Goods> data) {
        if (data != null) {
            this.mData = data;
            notifyDataSetChanged();
        }
    }
    
    public void removeItem(int position) {
        if (position >= 0 && position < mData.size()) {
            mData.remove(position);
            notifyItemRemoved(position);
        }
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_favorite, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Goods goods = mData.get(position);
        
        // 设置商品名称
        String goodsName = goods.getGoodsName();
        if (goodsName != null && !goodsName.isEmpty()) {
            holder.tvGoodsName.setText(goodsName);
        } else {
            holder.tvGoodsName.setText("商品名称未知");
        }
        
        // 设置价格
        holder.tvGoodsPrice.setText(String.format("¥%.2f", goods.getGoodsPrice()));
        
        // 加载商品图片 - 尝试多个图片字段
        String imageUrl = goods.getGoodsPic();
        if (imageUrl == null || imageUrl.trim().isEmpty()) {
            imageUrl = goods.getGoodsSmallLogo();
        }
        if (imageUrl == null || imageUrl.trim().isEmpty()) {
            imageUrl = goods.getGoodsBigLogo();
        }
        
        if (imageUrl != null && !imageUrl.trim().isEmpty()) {
            Glide.with(holder.itemView.getContext())
                    .load(imageUrl)
                    .placeholder(R.drawable.placeholder_image)
                    .error(R.drawable.placeholder_image)
                    .centerCrop()
                    .into(holder.ivGoodsImage);
        } else {
            holder.ivGoodsImage.setImageResource(R.drawable.placeholder_image);
        }
        
        // 商品点击
        holder.itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(goods);
            }
        });
        
        // 删除点击
        holder.ivDelete.setOnClickListener(v -> {
            if (mOnDeleteClickListener != null) {
                mOnDeleteClickListener.onDeleteClick(goods, position);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return mData.size();
    }
    
    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivGoodsImage;
        TextView tvGoodsName;
        TextView tvGoodsPrice;
        ImageView ivDelete;
        
        ViewHolder(View itemView) {
            super(itemView);
            ivGoodsImage = itemView.findViewById(R.id.iv_goods_image);
            tvGoodsName = itemView.findViewById(R.id.tv_goods_name);
            tvGoodsPrice = itemView.findViewById(R.id.tv_goods_price);
            ivDelete = itemView.findViewById(R.id.iv_delete);
        }
    }
}

